1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| data(){ return { data:[ { adCost: 0, channelName: "fb_campagin_1", chargeRevenue: null, country: "美国", createRoleNum: 0, time: "2019-04-26" }, { adCost: 1, channelName: "fb_campagin_1", chargeRevenue: null, country: "美国", createRoleNum: 1, time: "2019-04-26" }, { adCost: 3, channelName: "fb_campagin_1", chargeRevenue: null, country: "美国", createRoleNum: 3, time: "2019-04-26" }, { adCost: 3, channelName: "fb_campagin_1", chargeRevenue: null, country: "韩国", createRoleNum: 3, time: "2019-04-26" }, { country: "韩国", adCost: 3, channelName: "fb_campagin_1", chargeRevenue: null, createRoleNum: 3, time: "2019-04-26" } ] } }
method(){ init(){ //在某一个函数里初始化, this.setTable(this.data,'country') //这里会return一个对象 {'country': [3,0,0,2,0]} 之所以是对像是因为可能有多列需要合并行,便于区分 } /** *data 一维数组的数据 *c 列名 */ setTable(data, c) { let spanOneArr = [] let concatOne = 0 data.forEach((item, index) => { if (index === 0) { spanOneArr.push(1) } else { if (item[c] && item[c] === data[index - 1][c]) { //当前项和前一项比较 spanOneArr[concatOne] += 1 //相同值第一个出现的位置,统计需要合并多少行 spanOneArr.push(0)//新增一个被合并行 } else { spanOneArr.push(1) //否则不合并 concatOne = index//指向位移 } } }) var obj = {} obj[c] = spanOneArr this.arr.push(obj) } objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { const _row = this.arr[0].country[rowIndex] //因为rowIndex出现会从1到结尾 const _col = _row > 0 ? 1 : 0 return { rowspan: _row, colspan: _col } } } }
|